﻿using CustomModel;
using HeyPiPi_Desk.Base;
using Microsoft.Practices.Prism.Commands;
using Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDeskServiceBLL;
using DeskInterfaceBLL;
using LibrarySystem.Tools;
using HeyPiPi_Desk.Models;

namespace HeyPiPi_Desk.DomainViewModel
{
    public class AuthorityGroupViewModel : BaseNotification
    {
        #region 属性/字段
        private readonly IshopAuthorityBLL ishopAuthorityBLL = new ShopAuthorityBLL();

        private ObservableCollection<ShopAuthorityModel> _shopAuthorityList;

        /// <summary>
        /// 权限集合
        /// </summary>
        public ObservableCollection<ShopAuthorityModel> ShopAuthorityList
        {
            get { return _shopAuthorityList; }
            set { _shopAuthorityList = value; this.RaisePropertyChanged(() => this.ShopAuthorityList); }
        }


       /// <summary>
       /// 权限列表选择事件
       /// </summary>
        public DelegateCommand<object> AuthoritySelectedId_Cmd { get; set; }

        private ObservableCollection<shop_AuthorityGroup> _authorityItemList;

        /// <summary>
        /// 原始权限组列表
        /// </summary>
        public ObservableCollection<shop_AuthorityGroup> AuthorityItemList
        {
            get { return _authorityItemList; }
            set { _authorityItemList = value; this.RaisePropertyChanged(() => this.AuthorityItemList); }
        }



        /// <summary>
        /// 父权限勾选命令
        /// </summary>
        public DelegateCommand ParentCheck_Cmd { get; set; }
        /// <summary>
        /// 子权限勾选命令
        /// </summary>
        public DelegateCommand<object> ChildCheck_Cmd { get; set; }

        #endregion

        #region 私有方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _initFunc()
        {

            // 获得权限组列表
            this.AuthorityItemList = new ObservableCollection<shop_AuthorityGroup>();

            //    var  _oriAuthorityGroupList= this.ishopAuthorityBLL.GetAuthorityGroupByShopId(GlobalParam.shop.shopid);

            //测试数据
            List<shop_AuthorityGroup> g = new List<shop_AuthorityGroup>();
            shop_AuthorityGroup i = new shop_AuthorityGroup();
            i.Name = "测试权限";
            i.Id = 1;
            i.Items = "快速收银|自定义收银";

            g.Add(i);
            var _oriAuthorityGroupList = g;

            if (_oriAuthorityGroupList == null)
            {
                GlobalSetting.GetGlobalSetting().OpenTipMessage("您当前未设置权限组");
            }
            else
            {
                // todo:处理权限组

                this.AuthorityItemList = new ObservableCollection<shop_AuthorityGroup>(_oriAuthorityGroupList);
                this.ShopAuthorityList = new ObservableCollection<ShopAuthorityModel>();

            }

         

        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void _initCmd()
        {
            this.AuthoritySelectedId_Cmd = new DelegateCommand<object>((oid) =>
            {
                shop_AuthorityGroup item = oid as shop_AuthorityGroup;
                //根据id找到权限，然后显示
         
                List<string> _itemlist = new List<string>();
                if (item.Items!=null)
                {
                    string[] _temp = item.Items.Split(new char[] { '|'});
                    if (_temp.Length!=0)
                    {
                        for (int i = 0; i < _temp.Length; i++)
                        {
                            _itemlist.Add((_temp[i]));
                        }
                       this.ShopAuthorityList=  CreateMenu.GetInstance().GetAuthorityGroupModelList(_itemlist);
                    }
                    else
                    {
                        GlobalSetting.GetGlobalSetting().OpenTipMessage("该权限组没有权限项目");
                        return;
                    }
                }
                else
                {
                    GlobalSetting.GetGlobalSetting().OpenTipMessage("该权限组没有权限项目");
                    return;
                }

               

            });
        }




        #endregion

        #region 共有方法

        /// <summary>
        /// 构造方法
        /// </summary>
        public AuthorityGroupViewModel()
        {
            this._initFunc();
            this._initCmd();
        }

        #endregion

    }
}

